package top.infopub.tdp.util;


import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.infopub.model.sys.ParameterGroupDetail;
import top.infopub.tdp.sys.service.DataDicService;
import top.infopub.util.SpringContext;


/**
 * 系统参数数据字典 操作类
 * @author Awoke
 * @version 2018年9月20日
 * @see GrpParaUtil
 * @since
 */
public final class GrpParaUtil {

    private static Logger log = LoggerFactory.getLogger(GrpParaUtil.class);

    private static DataDicService service;

    /**
     * 根据key返回集合参数表中配置值
     * @param key
     * @return
     */
    public static Set<ParameterGroupDetail> get(String groupCode) {
        initBean();
        return service.selectDataDictInfo(groupCode).getDataDicDetails();
    }

    /**
     * 根据groupCode和名字查找值  sValue
     */
    public static String getValue(String groupCode, String name) {
        initBean();
        ParameterGroupDetail dtl = service.selectParamGroupDetailInfo(groupCode, name);
        if (dtl == null) {
            return "";
        }
        return dtl.getSvalue();
    }

    /**
     * 根据主项编号和子项值查找name sName
     */
    public static String getName(String groupCode, String value) {
        initBean();
        Set<ParameterGroupDetail> _vs = service.selectDataDictInfo(groupCode).getDataDicDetails();
        ParameterGroupDetail _dest = new ParameterGroupDetail();
        try {
            for (ParameterGroupDetail _d : _vs) {
                if (value.equalsIgnoreCase(_d.getSvalue())) {
                    BeanUtils.copyProperties(_dest, _d);
                    break;
                }
            }
        }
        catch (Exception e) {
            log.error("", e);
        }
        return _dest.getSname();
    }

    /**
     * 根据主项编号和子项值查找说明 sRemark
     */
    public static String getExplainForValue(String groupCode, String value) {
        initBean();
        Set<ParameterGroupDetail> _vs = service.selectDataDictInfo(groupCode).getDataDicDetails();
        ParameterGroupDetail _dest = new ParameterGroupDetail();
        try {
            for (ParameterGroupDetail _d : _vs) {
                if (value.equalsIgnoreCase(_d.getSvalue())) {
                    BeanUtils.copyProperties(_dest, _d);
                    break;
                }
            }
        }
        catch (Exception e) {
            log.error("", e);
        }
        return _dest.getSremark();
    }

    /**
     * 根据主项编号和子项名查找说明 sRemark
     */
    public static String getExplain(String groupCode, String name) {
        initBean();
        Set<ParameterGroupDetail> _vs = service.selectDataDictInfo(groupCode).getDataDicDetails();
        ParameterGroupDetail _dest = new ParameterGroupDetail();
        try {
            for (ParameterGroupDetail _d : _vs) {
                if (name.equalsIgnoreCase(_d.getSname())) {
                    BeanUtils.copyProperties(_dest, _d);
                    break;
                }
            }
        }
        catch (Exception e) {
            log.error("", e);
        }
        return _dest.getSremark();
    }

    /***
     * 初始化Bean
     */
    private static void initBean() {
        if (service == null) {
            service = SpringContext.getBean(DataDicService.class);
        }
    }

}
